$(document).ready(function () {
    init();

    function init() {
        $.ajax({
            type: "get",
            url: urlUserCenter + "/clazz/getClazz",
            dataType: "json",
            success: function (data) {
                for (var i in data) {
                    $("#inClazz").append("<option value=" + data[i].id + ">" + data[i].name + "</option>");
                }
            },
            error: function (err) {
                console.log(err);
            }
        })
    }

    $("#inClazz").change(function () {
        getUserByClazzId();
        intoGroup();
    });

    function getUserByClazzId() {
        $("#inUser1").empty();//首先清空select现在有的内容
        var clazzId = $("#inClazz").val();
        $.ajax({
            type: "get",
            url: urlUserCenter+"/user/getUserByClazzId/" + clazzId,
            dataType: "json",
            success: function (data) {
                for (var i in data) {
                    $("#inUser1").append("<option value=" + data[i].id + ">"
                        + data[i].name + "</option>");
                }
                intoUser2();
            },
            error: function (err) {
                console.log(err);
            }
        })
    }

    function intoUser2(){
        $("#inUser2").empty();//首先清空select现在有的内容
        var clazzId = $("#inClazz").val();
        var userId1=$("#inUser1").val();
        var user={
            clazzid:clazzId,
            id:userId1
        };
        $.ajax({
            async: false,     //是否为异步请求
            cache: false,  //是否缓存结果
            type:"post",
            url:urlUserCenter+"/user/getUser2ById",
            data:user,
            dataType:"json",
            success:function (data) {
                var users=data.users;
                if(users==null||users==""){
                    alert("没有其他学生！");
                }else{
                    for (var i in users) {
                        $("#inUser2").append("<option value=" + users[i].id + ">"
                            + users[i].name + "</option>");
                    }
                }
            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        })
    }
    
    $("#setGroup").click(function () {
        var userId1=$("#inUser1").val();
        var userId2=$("#inUser2").val();
        var clazzId=$("#inClazz").val();
        var userId={
          userId1:userId1,
          userId2:userId2,
        };
        $.ajax({
            type:"post",
            url:"/group/setGroup/"+userId1+"/"+userId2+"/"+clazzId,
            dataType:"json",
            success:function (data) {
                if(data.result=="exist"){
                    alert("该成员已被分组！");
                    return;
                }else if(data.result=="success"){
                    alert("结对分组成功!");
                    intoGroup();
                }else if(data.result=="false"){
                    alert("结对分组失败！");
                    return;
                }
            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        })
    });

    function intoGroup() {
        $("#groupTable tr:not(:first)").empty();//首先清空原来有的内容
        var clazzId=$("#inClazz").val();
        $.ajax({
            type:"get",
            url:"/group/inGroup/"+clazzId,
            dataType:"json",
            success:function (data) {
                var group=data.group;
                for(var i in group){
                    $("#groupTable").append("<tr><td>" + group[i].id
                        + " </td><td>" + group[i].userName1
                        + " </td><td>" + group[i].userName2
                        + " </td><td>" +"<input type='button' class='deleteGroup' value='移除'/>"
                        + "</td></tr>");
                }
                $(".deleteGroup").click(function () {
                    var groupId = $(this).parent().parent().find("td").eq(0).text();
                    deleteGroup(groupId);
                });
            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        })
    }

    function deleteGroup(data) {
        var groupId=data;
        $.ajax({
            type:"get",
            url:"/group/deleteGroup/"+groupId,
            dataType:"text",
            success:function (data) {
                if(data==0){
                    alert("删除失败！");
                }else if(data==1){
                    alert("删除成功！");
                    $(this).parent().parent().remove();
                    intoGroup();
                }
            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        })
    }


});